package whendone

/**
	Input/Output handling.
  Call close before exiting the application.
  @author Henrik Huttunen   
 */
trait IO{
  def print(message: String)
  def println(message: String)
  def readLine: String
  /** Closes resources. */ 
  def close = {}
  def init = {}    
}

trait ConsoleIO extends IO{
  def print(message: String) = Console.print(message)
  def println(message: String) = print(message + "\n")
  def readLine: String = Console.readLine
}

/**
  Logs io actions. Combined with real io, e.g. val io = RealIO with LoggerIO, where RealIO extends IO. 
	@author Henrik Huttunen
*/
trait LoggerIO extends IO{
  val file: String
  val output = new java.io.DataOutputStream(new java.io.FileOutputStream(file))
  
  abstract override def print(message: String) = {
    super.print(message)
    output.writeChars(message)
  }
  abstract override def println(message: String) = {
    super.println(message)
    // ignores loggin because printlns are created in terms of print
  }
  abstract override def readLine: String = {
    val message = super.readLine
    output.writeChars(message)
    output.writeChar('\n')
    message
  }
}